<?php


namespace App\Extensions;


class Content extends \Encore\Admin\Layout\Content
{
    protected $content_view = 'admin::content';
    protected $isPage = false;

    public function isPage(): Content
    {
        $this->isPage = true;
        return $this;
    }

    public function when($value, $callback, $default = null)
    {
        if ($value) {
            return $callback($this, $value) ?: $this;
        } elseif ($default) {
            return $default($this, $value) ?: $this;
        }

        return $this;
    }

    protected function setContentView($view): Content
    {
        $this->content_view = $view;
        return $this;
    }

    /**
     * @return array|string
     * @throws \Throwable
     */
    public function render()
    {
        $items = [
            'header'      => $this->title,
            'description' => $this->description,
            'breadcrumb'  => $this->breadcrumb,
            '_content_'   => $this->build(),
            '_view_'      => $this->view,
            '_user_'      => $this->getUserData(),
            'is_page'     => $this->isPage
        ];
        if (request()->has('_modal_')) {
            $this->setContentView('admin.content-modal');
        } elseif ($this->isPage || request()->pjax()) {
            $this->setContentView('admin.content-page');
        }
        return view($this->content_view, $items)->render();
    }

}
